package base;

import factory.ThreadPoolExecutorProxyFactory;
import holder.HomeHolder;
import holder.ItemHolder;
import holder.LoadMoreHolder;

import java.util.ArrayList;
import java.util.List;

import utils.UIUtils;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;

public abstract class SuperBaseAdapter<ITEMBEANTYPE> extends BaseAdapter
		implements OnItemClickListener {
	

	protected List<ITEMBEANTYPE> mDataSources;
	private int VIEWTYPE_LOADMORE = 0;
	private int VIEWTYPE_NORMAL = 1;
	private LoadMoreHolder loadMoreHolder;
	private int PAGESIZE = 20;
	private LoadMoreTask mLoadMoreTask;
	private AbsListView mABslistView;

	public SuperBaseAdapter(AbsListView ablistView, List<ITEMBEANTYPE> mDatas) {
		super();
		mABslistView = ablistView;
		mABslistView.setOnItemClickListener(this);
		this.mDataSources = mDatas;
	}

	@Override
	public int getCount() {
		if (mDataSources != null) {
			return mDataSources.size() + 1;
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if (mDataSources != null) {
			return mDataSources.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	// listView中显示几种不同的ViewType

	/**
	 * 得到ViewType的总数,默认是一种
	 */
	@Override
	public int getViewTypeCount() {
		return super.getViewTypeCount() + 1;
	}

	/**
	 * 通过position确定要显示的ViewType
	 */
	@Override
	public int getItemViewType(int position) {
		if (position == getCount() - 1) {
			return VIEWTYPE_LOADMORE;// 0
		} else {
			
//			return VIEWTYPE_NORMAL;// 1
			return getNormalItemViewType(position);
		}

	}

	/**
	 * 默认为1
	 * @param position
	 * @return
	 */
	protected  int getNormalItemViewType(int position) {
		 
		return VIEWTYPE_NORMAL;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		BaseHolder<ITEMBEANTYPE> baseHolder = null;
		if (convertView == null) {
			if (getItemViewType(position) == VIEWTYPE_LOADMORE) {
				// 返回加载更多的holder
				baseHolder = (BaseHolder<ITEMBEANTYPE>) getLoadMoreHolder();
			} else {
				// 返回一个BaseHolder的子类对象
				baseHolder = getSpecialHolder(position);
			}
		} else {
			baseHolder = (BaseHolder) convertView.getTag();
		}

		/*--------data 和视图的绑定-------*/
		if (getItemViewType(position) == VIEWTYPE_LOADMORE) {// 加载更多的视图
			// 加载更多的数据
			if (hasLoadMore()) {
				// 执行加载更多的数据
				triggerLoadMoreData();
			} else {
				// 显示没有更多的数据的视图
				loadMoreHolder
						.setDataAndRefreshHolderView(LoadMoreHolder.LOADING_NONE);
			}

		} else {
			ITEMBEANTYPE data = mDataSources.get(position);
			// 接收数据，绑定数据
			baseHolder.setDataAndRefreshHolderView(data);
		}
		return baseHolder.mHolderView;

	}

	/**
	 * 执行加载更多的数据
	 */
	private void triggerLoadMoreData() {
		if (mLoadMoreTask == null) {
			// 初始化mLoadMoreHolder视图
			int state = LoadMoreHolder.LOADING_MORE;
			loadMoreHolder.setDataAndRefreshHolderView(state);

			// 异步加载数据
			mLoadMoreTask = new LoadMoreTask();
			ThreadPoolExecutorProxyFactory.getNormalPoolExecutorProxy()
					.execute(mLoadMoreTask);
		}
	}

	/**
	 * 异步加载数据的任务
	 */
	class LoadMoreTask implements Runnable {
		@Override
		public void run() {
			int state;
			List<ITEMBEANTYPE> loadMoreData = null;

			try {
				// 获取数据
				loadMoreData = onLoadMore();

				if (loadMoreData == null) {
					state = LoadMoreHolder.LOADING_NONE;// 没有加载更多
				}
				if (loadMoreData.size() < PAGESIZE) {
					state = LoadMoreHolder.LOADING_NONE;// 没有加载更多
				} else {
					state = LoadMoreHolder.LOADING_MORE;// 可能有加载更多
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				state = LoadMoreHolder.LOADING_RETRY;
			}
			// 定义临时变量
			final int tempState = state;
			// 真正的在子线程中加载 更多
			final List<ITEMBEANTYPE> tempLoadMoreData = loadMoreData;

			// 根据当前数据刷新UI,要放置在主线程中
			UIUtils.postTaskSafely(new Runnable() {

				@Override
				public void run() {
					// 刷新 ui
					// 1.listView -->更新数据集 -->notifyDataSetChange方法
					if (tempLoadMoreData != null) {
						mDataSources.addAll(tempLoadMoreData);
						notifyDataSetChanged();
					}

					// 2.刷新mLoadMoreHolder
					loadMoreHolder.setDataAndRefreshHolderView(tempState);
				}
			});

			// 当run方法执行完，置空，方便下次可以进来
			mLoadMoreTask = null;

		}
	}

	/**
	 * 什么时候方法定义的地方，会往外抛，抛到方法调用处
	 */
	/**
	 * @des 真正在子线程中加载更多数据
	 * @des 默认返回null,但是子类可以选择性的复写该方法，放回具体的加载更多之后的数据
	 * @return
	 */
	protected List<ITEMBEANTYPE> onLoadMore() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	// 加载更多相关的逻辑
	/**
	 * 决定是否有更多数据
	 * 
	 * @call 滑到底的时候被调用，如果子类不复写，调用的是父类的方法
	 * @return
	 */
	protected boolean hasLoadMore() {
		// TODO Auto-generated method stub
		return true;
	}

	private LoadMoreHolder getLoadMoreHolder() {
		if (loadMoreHolder == null) {
			loadMoreHolder = new LoadMoreHolder();
		}
		return loadMoreHolder;
	}

	/**
	 * 返回holder的子类对象
	 * @param position 
	 * 
	 * @call 来到getView方法中，而且需要convertView等于null
	 * @return
	 */
	protected abstract BaseHolder<ITEMBEANTYPE> getSpecialHolder(int position);

	/*-----------  处理Item的点击事件 ----------*/
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (mABslistView instanceof ListView) {
			position = position - ((ListView)mABslistView).getHeaderViewsCount();
		}
		
		
		if (getItemViewType(position) == VIEWTYPE_LOADMORE) {
			// 重新去加载更多的数据
			// 触发加载更多的数据
			triggerLoadMoreData();
		} else {
			// 普通条目
			onNormalItemClick(parent, view, position, id);
		}
	}

	// 普通条目的点击事件
	public void onNormalItemClick(AdapterView<?> parent, View view,
			int position, long id) {

	}
}
